<template>
    <section class="dc-edit" v-focus>
        <XdoCard class="xdo-card" :bordered="false">
            <div class="xdo-card-body">
                <XdoForm ref="formItem" class="dc-form" :model="formItem" label-position="right" :label-width="120" :rules="ruleValidate" inline>
                </XdoForm>
            </div>
        </XdoCard>
        <div class="xdo-enter-action dc-edit-actions">
            <template v-for="item in buttons">
                <XdoButton v-if="item.needed === true" :type="item.type" :disabled="item.disabled"
                           :loading="item.loading"
                           @click="item.click" :icon="item.icon" :key="item.icon">{{ item.label }}
                </XdoButton>&nbsp;
            </template>
        </div>
    </section>
</template>
<script>
    import { jx_bill_testUri } from '@/api'
    import { editStatus } from '@/view/jx_bill_test-common/constant'
    import { decimalKeypress,decimalKeyup,numberKeypress,numberKeyup} from '@/libs/num'
    export default {
        name: 'DcDecLogDetailEdit',
        props: {
            editConfig: { type: Object, default: () => ({}) }
        },
        data() {
            return {
                showDisable:false,
                formItem: {
                    sid: '',
                },
                ruleValidate: {
                },
                buttons: [
                    {
                        code: 'Save',
                        type: 'primary',
                        disabled: false,
                        click: this.handleSave,
                        label: '保存',
                        loading: false,
                        needed: true
                    },
                    {
                        code: 'Cancel',
                        type: 'primary',
                        disabled: false,
                        click: this.handleBack,
                        label: '返回',
                        loading: false,
                        needed: true
                    }
                ],
            }
        },
        mounted: function(){
            if (this.editConfig && this.editConfig.editStatus === editStatus.ADD) {
                this.resetFormData()
                this.formItem.headId = this.editConfig.headId
                this.showDisable = false
                this.buttons.filter(item => item.code === "Save")[0].needed = true
            } else if (this.editConfig && this.editConfig.editStatus === editStatus.EDIT) {
                this.formItem = { ...this.editConfig.editData }
                this.formItem.headId = this.editConfig.headId
                this.showDisable = false
                this.buttons.filter(item => item.code === "Save")[0].needed = true
            } else if (this.editConfig && this.editConfig.editStatus === editStatus.SHOW) {
                this.formItem = { ...this.editConfig.editData }
                this.formItem.headId = this.editConfig.headId
                this.showDisable = true
                this.buttons.filter(item => item.code === "Save")[0].needed = false
            } else {
                this.$Message.error('缺失编辑信息!')
            }
        },
        methods: {
            resetFormData() {
                this.$refs['formItem'].resetFields()
            },
            handleSave() {
                this.$refs['formItem'].validate().then(isValid => {
                    if (isValid) {
                        let http = ''
                        this.buttons.filter(item => item.code === "Save")[0].loading = true
                        const sid = this.formItem.sid
                        if (sid) {
                            http = this.$http.put(`${jx_bill_testUri.logDetailManageUrl.dcDecLogDetail.update}/${sid}`, this.formItem, {noIntercept: true})
                        } else {
                            http = this.$http.post(jx_bill_testUri.logDetailManageUrl.dcDecLogDetail.insert, this.formItem, {noIntercept: true})
                        }
                        http.then(res => {
                            if (res.data.success) {
                                this.$Message.success(res.data.message)
                                this.handleBack()
                            } else {
                                this.$Message.error(res.data.message)
                            }
                        }, () => {}).finally(() => {
                            this.buttons.filter(item => item.code === "Save")[0].loading = false
                        })
                    }
                })
            },
            handleBack() {
                this.$emit('onEditBack', true)
            },
            decimalKeypress,decimalKeyup,numberKeypress,numberKeyup
        }
    }
</script>
<style lang="less" scoped>
</style>
